package com.lan.iot;


import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lan.iot.utils.JsonUtil;
import com.lan.iot.utils.OkHttpRequestUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
import okhttp3.Response;
import okhttp3.ResponseBody;

import static com.lan.iot.Activity_index.numChange;

/**
 * Created by Eve on 2022/10/19.
 */
public class RAdapterz extends RecyclerView.Adapter {
    private List<String> nData = new ArrayList<>();
    private List<Integer> imgData = new ArrayList<>();
    private List<Class> intentClass = new ArrayList<>();
    private List<Boolean> haveLo = new ArrayList<>();
    private Context context;
    NetWorkBusiness netWorkBusiness;
    static String token;
    private Boolean acdnIsOpen=false;
    private Boolean zIsOpen=false;
    private Boolean cIsOpen=false;

    private void init() {
        NetWorkBusiness netWorkBusiness2 = new NetWorkBusiness("", "http://www.nlecloud.com/");
        netWorkBusiness2.signIn(new SignIn("17680316790", "1234567890"), new NCallBack<BaseResponseEntity<User>>(context) {
            @Override
            protected void onResponse(BaseResponseEntity<User> response) {
                netWorkBusiness = new NetWorkBusiness(response.getResultObj().getAccessToken(), "http://www.nlecloud.com/");
                token = response.getResultObj().getAccessToken();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        NlecloudUtil nlecloudUtil=new NlecloudUtil();
//                        nlecloudUtil.gainDevices("596248", token, new NlecloudResponse<List<SensorInfoEntity>>() {
//                            @Override
//                            public void callBack(ResultEntity<List<SensorInfoEntity>> resultEntity) {
//                                List<SensorInfoEntity> resultObj = resultEntity.getResultObj();
//                            }
//                        });
                    }
                }).start();
            }
        });
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0x01:
                    int status = msg.arg1;
                    String msgText = "";
                    if (status == 0) {
                        msgText = "控制失败:" + msg.obj;
                    } else if (status == 1) {
                        msgText = "控制成功:" + msg.obj;
                    }
                    Toast.makeText(context, msgText, Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };
    private void controlDevice(String deviceId, String apiTag, String data) {
        //发出网络请求
        String SENSOR_DATA_ALL = "http://api.nlecloud.com/Cmds";
        try {
            Map<String, String> valmap = new HashMap<>();
            valmap.put("deviceId", deviceId);
            valmap.put("apiTag", apiTag);
            Response access = OkHttpRequestUtil.postAccess(SENSOR_DATA_ALL, valmap, data, token);
            if (access.isSuccessful()) {
                ResponseBody body = access.body();
                Map map = JsonUtil.toMap(body.string());
                Double status = (double) map.get("Status");
                Message message = new Message();
                if (status == null || status.intValue() != 0.0) {
                    message.arg1 = 0;
                } else {
                    message.arg1 = 1;
                }
                message.obj = map.get("Msg");
                message.what = 0x01;
                handler.sendMessage(message);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

    public RAdapterz(Context context) {
        this.context = context;

        //item中的数据
//        mData.add("客户联系");
//        mData.add("客户群");
//        mData.add("微信客服");
//        mData.add("离职继承");
//        mData.add("群发助手");
//        mData.add("对外收款");

        nData.add("照明");
        nData.add("空调");
        nData.add("窗帘");
//        nData.add("音响");

        imgData.add(R.drawable.light_200);
        imgData.add(R.drawable.acd);
        imgData.add(R.drawable.chuanglain200);
//        imgData.add(R.drawable.music);

        intentClass.add(light.class);
        intentClass.add(AirCdn.class);
        intentClass.add(null);
//        intentClass.add(Music.class);
        haveLo.add(false);
        haveLo.add(true);
        haveLo.add(false);
//        haveLo.add(true);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        int layoutId;
        //加载item的布局文件
        layoutId = R.layout.item_one_view_layout;
        view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final VH vh = (VH) holder;
//        vh.mText.setText(mData.get(position) + "");
        vh.nText.setText(nData.get(position) + "");
        vh.nText.setTextColor(0xff000000);
        vh.img.setImageResource(imgData.get(position));
        if(position==1){vh.sw.setChecked(acdnIsOpen);

        vh.sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                numChange("zkongtiao",b);

            }
        });}
        if(position==0)vh.sw.setChecked(zIsOpen);
        if(position==2)vh.sw.setChecked(cIsOpen);
        if (position == 2) {
            init();
            vh.sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    numChange("zchuanglian",b);
                    if (b) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                controlDevice("638233", "chuang", "1");
                                //airconditionControl();
                            }
                        }).start();
                    } else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                controlDevice("638233", "chuang", "0");

                                //airconditionControl();
                            }
                        }).start();
                    }
                }
            });
        }
//        if (position == 3) {
//            vh.sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                    if (b) {
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                controlDevice("638233", "c1", "1");
//                                //airconditionControl();
//                            }
//                        }).start();
//                    } else {
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                controlDevice("638233", "c1", "0");
//                                //airconditionControl();
//                            }
//                        }).start();
//                    }
//                }
//            });
//        }
        if (position == 0) {
            vh.sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    numChange("zdeng",b);
                    if (b) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                controlDevice("638233", "denger", "1");

                                //airconditionControl();
                            }
                        }).start();
                    } else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                controlDevice("638233", "denger", "0");
                                //airconditionControl();
                            }
                        }).start();
                    }
                }
            });
        }
        //设置item的点击事件
        //这里只设置了第一个item的，有需要的自己添加；
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (haveLo.get(position)) {
                    Intent intent = new Intent(context, intentClass.get(position));
                    intent.putExtra("name", vh.sw.isChecked());
                    intent.putExtra("kt", "kt2");
                    context.startActivity(intent);
                }else{
                    Toast to = Toast.makeText(context, "此设备没有控制界面", Toast.LENGTH_SHORT);
                    to.setGravity(Gravity.CENTER, 0, 0);
                    to.show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return nData.size();
    }

    public void refreshAcdn(Boolean msg_a) {
        acdnIsOpen = msg_a;
        notifyDataSetChanged();
    }

    public void refreshz(Boolean msg_a) {
        zIsOpen = msg_a;
        notifyDataSetChanged();
    }

    public void refreshc(Boolean msg_a) {
        cIsOpen = msg_a;
        notifyDataSetChanged();
    }

    //初始化item布局文件的各个控件
    class VH extends RecyclerView.ViewHolder {
        TextView mText, nText;
        ImageView img;
Switch sw;
        public VH(@NonNull View itemView) {
            super(itemView);
            sw=itemView.findViewById(R.id.sw);
            nText = itemView.findViewById(R.id.text1);
//            nText.setText("111");
            img = itemView.findViewById(R.id.img);
        }
    }


}
